package com.xinqi.modules.prepare.prepare.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.prepare.prepare.domain.PrepareEntity;
import com.xinqi.modules.prepare.prepare.domain.PrepareMemberEntity;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareMemberQueryDTO;
import com.xinqi.modules.prepare.prepare.dto.rsp.PrepareMemberResultDTO;
import com.xinqi.modules.prepare.prepare.enums.PrepareMemberRoleEnum;
import com.xinqi.modules.prepare.prepare.manager.PrepareMemberManager;
import com.xinqi.modules.prepare.prepare.mapper.PrepareMemberMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 备课区成员管理层实现
 *
 * @author: system
 * @date: 2023/03/19
 */
@Service
@RequiredArgsConstructor
public class PrepareMemberManagerImpl extends BaseServiceImpl<PrepareMemberMapper, PrepareMemberEntity> implements PrepareMemberManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<PrepareMemberResultDTO> findPage(Page<Object> page, PrepareMemberQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<PrepareMemberResultDTO> findList(PrepareMemberQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 备课区成员数据
     */
    @Override
    public PrepareMemberEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 备课区成员数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public PrepareMemberEntity findByPrepareIdAndUserId(Long prepareId, Long userId) {
        LambdaQueryWrapper<PrepareMemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareMemberEntity::getPrepareId, prepareId);
        wrapper.eq(PrepareMemberEntity::getUserId, userId);
        return getOne(wrapper);
    }

    @Override
    public boolean existByPrepareIdAndUserId(Long prepareId, Long userId) {
        LambdaQueryWrapper<PrepareMemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareMemberEntity::getPrepareId, prepareId);
        wrapper.eq(PrepareMemberEntity::getUserId, userId);
        return count(wrapper) > 0;
    }

    @Override
    public Boolean deleteByIds(Long prepareId, List<Long> ids) {
        LambdaQueryWrapper<PrepareMemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareMemberEntity::getPrepareId, prepareId);
        wrapper.in(PrepareMemberEntity::getId, ids);
        wrapper.ne(PrepareMemberEntity::getRole, PrepareMemberRoleEnum.LEADER.getCode());
        return remove(wrapper);
    }

    @Override
    public void addLeader(PrepareEntity prepare) {
        addLeader(prepare.getId(), prepare.getUserId());
    }

    @Override
    public void addLeader(Long prepareId, Long userId) {
        PrepareMemberEntity entity = new PrepareMemberEntity();
        entity.setPrepareId(prepareId);
        entity.setRole(PrepareMemberRoleEnum.LEADER);
        entity.setUserId(userId);
        entity.setJoinTime(LocalDateTime.now());
        save(entity);
    }

    @Override
    public long countByPrepareId(Long prepareId, PrepareMemberRoleEnum role) {
        LambdaQueryWrapper<PrepareMemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareMemberEntity::getPrepareId, prepareId);
        wrapper.eq(role != null, PrepareMemberEntity::getRole, role.getCode());
        return count(wrapper);
    }

    @Override
    public long countByUserId(Long userId, PrepareMemberRoleEnum role) {
        LambdaQueryWrapper<PrepareMemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareMemberEntity::getUserId, userId);
        wrapper.eq(role != null, PrepareMemberEntity::getRole, role.getCode());
        return count(wrapper);
    }

}
